package com.midea.ai.air.ui.more;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.ui.more.adapter.MoreSettingBrainpowerSelectRoomAdapter;
import com.midea.ai.air.ui.more.data.FamilyMember;
import com.midea.ai.air.view.ListViewForScrollView;
import com.midea.ai.air.view.wheelview.ArrayWheelAdapter;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hww on 2016/4/20.
 * 新建联动-启动条件
 */
public class MoreSettingBrainpowerStartCondition extends BaseActivity implements View.OnClickListener {


    private RelativeLayout bar_Rl;
    private LinearLayout back_left;
    private ListViewForScrollView mListView, mListView_indoor, mListView_outdoor;
    private List<FamilyMember> mList_Datas = new ArrayList<>();
    private List<FamilyMember> mList_Datas_indoor = new ArrayList<>();
    private List<FamilyMember> mList_Datas_outdoor = new ArrayList<>();

    private View view_outDoor_temp, view_outDoor_humidity, view_outDoor_pm, view_inDoor_temp, view_inDoor_humidity, view_inDoor_pm;
    private PopupWindow pop_outDoor_temp, pop_outDoor_humidity, pop_outDoor_pm;//室内相关pop
    private PopupWindow pop_inDoor_temp, pop_inDoor_humidity, pop_inDoor_pm;  //室外相关pop


    private WheelView wl_indoor_temp_one, wl_indoor_temp_two, wl_indoor_humidity_one, wl_indoor_humidity_two, wl_indoor_pm_one, wl_indoor_pm_two;
    private WheelView wl_outdoor_temp_one, wl_outdoor_temp_two, wl_outdoor_humidity_one, wl_outdoor_humidity_two, wl_outdoor_pm_one, wl_outdoor_pm_two;

    String selected_String[] = new String[]{"低于", "高于"};
    String pm_String[] = new String[]{"10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "120", "140", "160", "180", "200", "220", "240", "260", "280", "300",};
    String temp_String[] = new String[]{"5℃", "6℃", "7℃", "8℃", "9℃", "10℃", "11℃", "12℃", "13℃", "14℃", "15℃", "16℃", "17℃", "18℃", "19℃", "20℃", "21℃", "22℃", "23℃", "24℃", "25℃", "26℃", "27℃", "28℃", "29℃", "30℃", "31℃", "32℃", "33℃", "34℃", "35℃"};
    String humidity_String[] = new String[]{"5%", "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%", "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettingbrainpowerstartcondition);

        getView_POP();
        init();
        setWheelView();
    }


    private void init() {
        bar_Rl = (RelativeLayout) findViewById(R.id.bar_parent_rl);
        ViewGroup.LayoutParams LP = bar_Rl.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        bar_Rl.setLayoutParams(LP);

        back_left = (LinearLayout) findViewById(R.id.left_relativeLayout_back);
        back_left.setOnClickListener(this);

        mListView = (ListViewForScrollView) findViewById(R.id.more_setting_brainpower_start_condition_one_listView);
        mListView_indoor = (ListViewForScrollView) findViewById(R.id.more_setting_brainpower_start_condition_three_listView);
        mListView_outdoor = (ListViewForScrollView) findViewById(R.id.more_setting_brainpower_start_condition_two_listView);

        mList_Datas.add(new FamilyMember(R.drawable.more_setting_branpower_big_01, "启动时间"));

        mList_Datas_outdoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_02, "室外温度"));
        mList_Datas_outdoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_03, "室外湿度"));
        mList_Datas_outdoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_04, "室外PM2.5"));

        mList_Datas_indoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_06, "室内温度"));
        mList_Datas_indoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_07, "室内湿度"));
        mList_Datas_indoor.add(new FamilyMember(R.drawable.more_setting_branpower_big_08, "室内PM2.5"));


        mListView.setAdapter(new MoreSettingBrainpowerSelectRoomAdapter(MoreSettingBrainpowerStartCondition.this, mList_Datas));
        mListView_outdoor.setAdapter(new MoreSettingBrainpowerSelectRoomAdapter(MoreSettingBrainpowerStartCondition.this, mList_Datas_outdoor));
        mListView_indoor.setAdapter(new MoreSettingBrainpowerSelectRoomAdapter(MoreSettingBrainpowerStartCondition.this, mList_Datas_indoor));


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(MoreSettingBrainpowerStartCondition.this, MoreSettingBrainpowerStartCondition_StartTime.class));
            }
        });

        //室外
        mListView_outdoor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                setResult(MoreSettingNewBrainpower.RESULT_SUCCESS);
//                MoreSettingBrainpowerStartCondition.this.finish();
                switch (position) {
                    case 0:
                        pop_outDoor_temp = Creat_Popuwindow(view, view_outDoor_temp);
                        break;
                    case 1:
                        pop_outDoor_humidity = Creat_Popuwindow(view, view_outDoor_humidity);
                        break;
                    case 2:
                        pop_outDoor_pm = Creat_Popuwindow(view, view_outDoor_pm);
                        break;
                    default:
                        break;
                }
            }
        });

        //室内
        mListView_indoor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        pop_inDoor_temp = Creat_Popuwindow(view, view_inDoor_temp);
                        break;
                    case 1:
                        pop_inDoor_humidity = Creat_Popuwindow(view, view_inDoor_humidity);
                        break;
                    case 2:
                        pop_inDoor_pm = Creat_Popuwindow(view, view_inDoor_pm);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    //获取显示pop的View
    private void getView_POP() {
        view_inDoor_temp = getPOP_View();
        view_inDoor_humidity = getPOP_View();
        view_inDoor_pm = getPOP_View();
        view_outDoor_temp = getPOP_View();
        view_outDoor_humidity = getPOP_View();
        view_outDoor_pm = getPOP_View();
    }

    private View getPOP_View() {
        LayoutInflater mLI = LayoutInflater.from(this);
        View convertView = mLI.inflate(R.layout.item_popupwindow_wheelview, null);
        return convertView;
    }

    /**
     * 设置WheelView的显示数据
     */
    private void setWheelView() {


        //室内相关WheelView

        // 室内温度
        wl_indoor_temp_one = (WheelView) view_inDoor_temp.findViewById(R.id.item_pop_one);
        wl_indoor_temp_two = (WheelView) view_inDoor_temp.findViewById(R.id.item_pop_two);
        wl_indoor_temp_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_indoor_temp_two.setCyclic(true);
        wl_indoor_temp_two.setAdapter(new ArrayWheelAdapter<>(temp_String));

        wl_indoor_temp_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内温度(高低) 结果：" + selected_String[index]);
            }
        });

        wl_indoor_temp_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内温度(℃) 结果：" + temp_String[index]);
            }
        });

        // 室内湿度
        wl_indoor_humidity_one = (WheelView) view_inDoor_humidity.findViewById(R.id.item_pop_one);
        wl_indoor_humidity_two = (WheelView) view_inDoor_humidity.findViewById(R.id.item_pop_two);
        wl_indoor_humidity_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_indoor_humidity_two.setAdapter(new ArrayWheelAdapter<>(humidity_String));
        wl_indoor_humidity_two.setCyclic(true);

        wl_indoor_humidity_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内湿度(高低) 结果：" + selected_String[index]);
            }
        });

        wl_indoor_humidity_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内湿度(%) 结果：" + humidity_String[index]);
            }
        });


        // 室内PM2.5
        wl_indoor_pm_one = (WheelView) view_inDoor_pm.findViewById(R.id.item_pop_one);
        wl_indoor_pm_two = (WheelView) view_inDoor_pm.findViewById(R.id.item_pop_two);
        wl_indoor_pm_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_indoor_pm_two.setAdapter(new ArrayWheelAdapter<>(pm_String));
        wl_indoor_pm_two.setCyclic(true);

        wl_indoor_pm_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内PM2.5(高低) 结果：" + selected_String[index]);
            }
        });

        wl_indoor_pm_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室内PM2.5() 结果：" + pm_String[index]);
            }
        });


        //室外相关WheelView

        //室外温度
        wl_outdoor_temp_one = (WheelView) view_outDoor_temp.findViewById(R.id.item_pop_one);
        wl_outdoor_temp_two = (WheelView) view_outDoor_temp.findViewById(R.id.item_pop_two);
        wl_outdoor_temp_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_outdoor_temp_two.setAdapter(new ArrayWheelAdapter<>(temp_String));
        wl_outdoor_temp_two.setCyclic(true);

        wl_outdoor_temp_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外温度(高低) 结果：" + selected_String[index]);
            }
        });

        wl_outdoor_temp_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外温度(℃) 结果：" + temp_String[index]);
            }
        });


        //室外湿度
        wl_outdoor_humidity_one = (WheelView) view_outDoor_humidity.findViewById(R.id.item_pop_one);
        wl_outdoor_humidity_two = (WheelView) view_outDoor_humidity.findViewById(R.id.item_pop_two);
        wl_outdoor_humidity_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_outdoor_humidity_two.setAdapter(new ArrayWheelAdapter<>(humidity_String));
        wl_outdoor_humidity_two.setCyclic(true);

        wl_outdoor_humidity_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外湿度(高低) 结果：" + selected_String[index]);
            }
        });

        wl_outdoor_humidity_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外湿度(%) 结果：" + humidity_String[index]);
            }
        });


        //室外PM2.5
        wl_outdoor_pm_one = (WheelView) view_outDoor_pm.findViewById(R.id.item_pop_one);
        wl_outdoor_pm_two = (WheelView) view_outDoor_pm.findViewById(R.id.item_pop_two);
        wl_outdoor_pm_one.setAdapter(new ArrayWheelAdapter<>(selected_String));
        wl_outdoor_pm_two.setAdapter(new ArrayWheelAdapter<>(pm_String));
        wl_outdoor_pm_two.setCyclic(true);

        wl_outdoor_pm_one.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外PM2.5(高低) 结果：" + selected_String[index]);
            }
        });

        wl_outdoor_pm_two.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "室外PM2.5() 结果：" + pm_String[index]);
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_relativeLayout_back:
                //        setResult(MoreSettingNewBrainpower.RESULT_FAILED);//反馈设置启动条件成功
                setResult(MoreSettingNewBrainpower.RESULT_SUCCESS);//反馈设置启动条件成功
                this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //设置弹窗POP的背景颜色
    private void setAttributes(float f) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = f;
        getWindow().setAttributes(lp);
    }

    /**
     * 新建 弹窗 POP
     *
     * @param v
     * @param convertView
     * @return
     */
    private PopupWindow Creat_Popuwindow(View v, View convertView) {
        PopupWindow popupWindow = null;
        if (popupWindow == null) {
            // LayoutInflater mLI = LayoutInflater.from(this);
            // View convertView = mLI.inflate(resource, null);
            popupWindow = new PopupWindow(convertView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

        }

        ColorDrawable cD = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cD);
        // popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 产生背景变暗效果
        setAttributes(0.2f);

        popupWindow.setFocusable(true);// 设置可获取焦点
        popupWindow.setOutsideTouchable(true);// 设置点击外部可dismiss
        popupWindow.setAnimationStyle(R.style.pop_anim_style);
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        popupWindow.update();

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                setAttributes(1f);
            }
        });

        return popupWindow;
    }


    @Override
    public void onBackPressed() {
        setResult(MoreSettingNewBrainpower.RESULT_FAILED);
        this.finish();
    }
}
